library(tidyverse)
library(showtext)
library(magrittr)
library(ggbeeswarm)
library(estimatr)

# read necessary data

c(
  "https://github.com/thomasjwood/cgptw22/raw/main/all_panels_long.rds",
  "https://github.com/thomasjwood/cgptw22/raw/main/treatments_df.rds"
  ) %>% 
  map2(
    str_c("t", 0:1),
    \(i, j)
    i %>% 
      url %>% 
      gzcon %>% 
      readRDS %>% 
      assign(
        x = j, 
        envir = .GlobalEnv
      )
  )

filter_and_flip <- function(data) {
  data %>%
    filter(term %in% c("treatmentcontrol", "treatmentfactcheck")) %>%
    mutate(
      estimate = if_else(term == "treatmentcontrol",-1 * estimate, estimate),
      conf.low = if_else(term == "treatmentcontrol",-1 * conf.low, conf.low),
      conf.high = if_else(term == "treatmentcontrol",-1 * conf.high, conf.high),
    )
  
}

t2 <- t0 %>%
  group_by(fc, panel, panel_factor, rating) %>%
  do(tidy(lm_robust(formula(
    paste0("outcome_w1 ~ treatment")
  ), data = .))) %>%
  filter_and_flip


# x values for ordering on figure

t0$treatment %<>% 
  factor(
    c("control",
      "misinformation",
      "factcheck")
  )

# arranging topic_short_2

t1$topic_short_2 %<>%
  factor(
    t1 %>% 
      arrange(date_fielded, panel) %>% 
      use_series(topic_short_2)
    )

t0$topic_short_2 %<>%
  factor(
    t1 %>% 
      arrange(date_fielded, panel) %>% 
      use_series(topic_short_2)
  )

# labels for constrast significance, restricted to significance
t2 %<>% 
  left_join(
    t1 %>% 
      select(
        panel, fc, topic_short_2
      )
    ) %>% 
  mutate(
    lab = c(
      "***", "**", "*", ""
      ) %>% 
      extract(
        p.value %>% 
          findInterval(
            c(-Inf, .001, .01, .05, Inf)
          )
      )
    ) %>% 
  filter(
    p.value %>%
      is_less_than(.05)
  )

# means and confidence intervals by condition
  
t3 <- t0 %>%
  group_by(
    treatment, topic_short_2
    ) %>%
  summarize(
    mu = outcome_w1 %>% 
      mean(na.rm = TRUE),
    se = outcome_w1 %>% 
      plotrix::std.error(na.rm = T)
    ) %>% 
  mutate(
    lo = mu %>% 
      subtract(se %>% multiply_by(1.96)),
    hi = mu %>% 
      add(se %>% multiply_by(1.96))
    ) %>%
  ungroup 


# contrasts for control vs misinfo
t2 %<>% 
  filter(
    term %>% 
      str_detect(
        "control"
      )
  ) %>% 
  left_join(
    t3 %>% 
      filter(
        treatment  %>% 
          str_detect("control")
        ) %>% 
      select(
        topic_short_2, hi
        ) %>% 
      rename(
        left_y = hi
        )
    ) %>% 
  left_join(
    t3 %>% 
      filter(
        treatment %>% 
          str_detect("misinformation")
        ) %>% 
      select(
        topic_short_2, hi
      ) %>% 
      rename(
        right_y = hi
      )
  ) %>% 
  bind_rows(
    # contrasts for misinfo vs correction
    t2 %>% 
      filter(
        term %>% 
          str_detect(
            "control"
          ) %>% 
          not
        ) %>% 
      left_join(
        t3 %>% 
          filter(
            treatment %>% 
              equals("misinformation")
            ) %>% 
          select(
            topic_short_2, hi
          ) %>% 
          rename(
            left_y = hi
          )
        ) %>% 
      left_join(
        t3 %>% 
          filter(
            treatment %>% 
              str_detect(
                "factcheck"
              )
            ) %>% 
          select(
            topic_short_2, hi
          ) %>% 
          rename(
            right_y = hi
          )
        )
    )

# Figure 2 ----------------------------------------------------------------

t0 %>%
  ggplot() +
  geom_quasirandom(
    aes(
      x = treatment, 
      y = outcome_w1
    ),
    shape = 21,
    fill  = "grey20",
    groupOnX = T,
    alpha = .075, 
    width = .3
  ) +
  geom_rect(
    aes(
      xmin = treatment %>% 
        as.numeric %>% 
        subtract(.01),
      xmax = treatment %>% 
        as.numeric %>% 
        add(.01),
      ymin = lo %>% 
        subtract(.3), 
      ymax = hi %>% 
        add(.3), 
    ),
    fill = "white",
    color = "white",
    data = t3
  ) +
  geom_segment(
    aes(
      treatment, y = left_y + 3, xend = treatment, yend = 80
    ),
    data = t2 %>% 
      filter(
        term %>% 
          equals("treatmentcontrol")
      ) %>% 
      mutate(
        treatment = "control" %>% 
          factor(
            t3$treatment %>% 
              levels
          )
        ),
    size = 1.17,
    color = "white",
    lineend = "square"
  ) +
  geom_segment(
    aes(
      treatment, y = right_y + 3, xend = treatment, yend = 80
    ),
    data = t2 %>% 
      filter(
        term %>% 
          equals("treatmentcontrol")
        ) %>% 
      mutate(
        treatment = "misinformation" %>% 
          factor(
            t3$treatment %>% 
              levels
            )
        ),
    size = 1.17,
    color = "white",
    lineend = "square"
  ) +
  geom_segment(
    aes(
      ltreatment, y = 80, xend = rtreatment, yend = 80
    ),
    data = t2 %>% 
      filter(
        term %>% 
          equals("treatmentcontrol")
      ) %>% 
      mutate(
        ltreatment = "control" %>% 
          factor(
            t3$treatment %>% 
              levels
          ),
        rtreatment = "misinformation" %>% 
          factor(
            t3$treatment %>% 
              levels
          )
      ),
    size = 1.17,
    color = "white",
    lineend = "square"
  ) +
  geom_segment(
    aes(
      ltreatment, y = 80, xend = rtreatment, yend = 80
    ),
    data = t2 %>%
      filter(
        term %>%
          str_detect("treatmentcontrol") %>%
          not
        ) %>%
      mutate(
        ltreatment = "misinformation" %>%
          factor(
            t3$treatment %>%
              levels
          ),
        rtreatment = "factcheck" %>%
          factor(
            t3$treatment %>%
              levels
            )
        ),
    size = 1.17,
    color = "white",
    lineend = "square"
  ) +
  geom_segment(
    aes(
      treatment, y = left_y + 3, xend = treatment, yend = 80
    ),
    data = t2 %>%
      filter(
        term %>%
          str_detect("treatmentcontrol") %>%
          not
      ) %>%
      mutate(
        treatment = "misinformation" %>%
          factor(
            t3$treatment %>%
              levels
          )
      ),
    size = 1.17,
    color = "white",
    linetype = "solid"
  ) +
  geom_segment(
    aes(
      treatment, y = right_y + 3, xend = treatment, yend = 80
    ),
    data = t2 %>%
      filter(
        term %>%
          str_detect("treatmentcontrol") %>%
          not
      ) %>%
      mutate(
        treatment = "factcheck" %>%
          factor(
            t3$treatment %>%
              levels
          )
      ),
    size = 1.17,
    color = "white",
    linetype = "solid"
  ) +
  geom_segment(
    aes(
      treatment, y = left_y + 3, xend = treatment, yend = 80
    ),
    data = t2 %>% 
      filter(
        term %>% 
          str_detect("control")
        ) %>% 
      mutate(
        treatment = "control" %>% 
          factor(
            t3$treatment %>% 
              levels
            )
        ),
    size = .24,
    linetype = "solid",
    color = "black",
    lineend = "square"
  ) +
  geom_segment(
    aes(
      treatment, y = right_y + 3, xend = treatment, yend = 80
    ),
    data = t2 %>% 
      filter(
        term %>% 
          str_detect("control")
        ) %>% 
      mutate(
        treatment = "misinformation" %>% 
          factor(
            t3$treatment %>% 
              levels
            )
        ),
    size = .24,
    linetype = "solid",
    color = "black",
    lineend = "square"
  ) +
  geom_segment(
    aes(
      ltreatment, y = 80, xend = rtreatment, yend = 80
    ),
    data = t2 %>% 
      filter(
        term %>% 
          str_detect("control")
      ) %>% 
      mutate(
        ltreatment = "control" %>% 
          factor(
            t3$treatment %>% 
              levels
          ),
        rtreatment = "misinformation" %>% 
          factor(
            t3$treatment %>% 
              levels
          )
      ),
    size = .24,
    linetype = "solid",
    color = "black",
    lineend = "square"
  ) +
  geom_segment(
    aes(
      treatment, y = left_y + 3, xend = treatment, yend = 80
    ),
    data = t2 %>%
      filter(
        term %>%
          str_detect("control") %>% 
          not
      ) %>%
      mutate(
        treatment = "misinformation" %>%
          factor(
            t3$treatment %>%
              levels
          )
      ),
    size = .24,
    color = "black",
    linetype = "solid",
    lineend = "square"
  ) +
  geom_segment(
    aes(
      treatment, y = right_y  + 3, xend = treatment, yend = 80
    ),
    data = t2 %>%
      filter(
        term %>%
          str_detect("control") %>% 
          not
      ) %>%
      mutate(
        treatment = "factcheck" %>%
          factor(
            t3$treatment %>%
              levels
          )
      ),
    size = .24,
    color = "black",
    linetype = "solid",
    lineend = "square"
  ) +
  geom_segment(
    aes(
      ltreatment, y = 80, xend = rtreatment, yend = 80
    ),
    data = t2 %>%
      filter(
        term %>%
          str_detect("control") %>% 
          not
      ) %>%
      mutate(
        ltreatment = "misinformation" %>%
          factor(
            t3$treatment %>%
              levels
          ),
        rtreatment = "factcheck" %>%
          factor(
            t3$treatment %>%
              levels
          )
      ),
    size = .24,
    color = "black",
    linetype = "solid",
    lineend = "square"
  ) +
  geom_text(
    aes(
      xnum, 83, label = lab
    ),
    size = 3,
    data = t2 %>% 
      mutate(
        xnum = term %>% 
          str_detect(
            "control"
          ) %>% 
          ifelse(
            1.5, 2.5
          ) %>% 
          as.numeric
      )
  ) +
  geom_linerange(
    aes(
      treatment, ymin = lo, ymax = hi, 
    ),
    data = t3 
  ) +
  geom_point(
    aes(
      treatment, mu
    ),
    shape = 21,
    size =  7,
    fill = "white",
    data = t3
  ) +
  geom_text(
    aes(
      treatment, mu, label = mu %>% round(0)
    ),
    data = t3,
    size = 2.65
    ) +
  facet_wrap(
    ~topic_short_2,
    ncol = 3, 
    labeller = label_wrap_gen(30)
  ) +
  labs(
    x = "Experimental condition",
    y = "Certainty of agreement\n(larger values indicate certain agreement, smaller value certain disagreement)",
    fill = "Experimental estimate"
  ) +
  scale_x_discrete(
    breaks = t3$treatment %>% 
      levels,
    labels = c(
      "Control",
      "Misinformation",
      "Fact-check"
    )
  ) 